﻿// Decompiled with JetBrains decompiler
// Type: Bannerlord.BUTR.Shared.Helpers.ApplicationVersionHelper
// Assembly: MCMv5, Version=5.10.2.0, Culture=neutral, PublicKeyToken=null
// MVID: 7AB326B9-B2B4-41C2-9177-68E944071101
// Assembly location: D:\steam\steamapps\workshop\content\261550\2859238197\bin\Win64_Shipping_Client\MCMv5.dll

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using TaleWorlds.Library;

#nullable enable
namespace Bannerlord.BUTR.Shared.Helpers
{
  [ExcludeFromCodeCoverage]
  [DebuggerNonUserCode]
  internal static class ApplicationVersionHelper
  {
    public static bool TryParse(string? versionAsString, out ApplicationVersion version)
    {
      return ApplicationVersionHelper.TryParse(versionAsString, out version, true);
    }

    public static bool TryParse(string? versionAsString, out ApplicationVersion version, bool asMin)
    {
      version = new ApplicationVersion();
      int result1 = asMin ? 0 : int.MaxValue;
      int result2 = asMin ? 0 : int.MaxValue;
      int result3 = asMin ? 0 : int.MaxValue;
      int result4 = asMin ? 0 : int.MaxValue;
      if (versionAsString == null)
        return false;
      string[] strArray = versionAsString.Split('.');
      if (strArray.Length != 3 && strArray.Length != 4 && strArray[0].Length == 0)
        return false;
      bool flag = false;
      if (!flag && !int.TryParse(strArray[0].Substring(1), out result1))
      {
        if (strArray[0].Substring(1) != "*")
          return false;
        result1 = int.MinValue;
        result2 = int.MinValue;
        result3 = int.MinValue;
        result4 = int.MinValue;
        flag = true;
      }
      if (!flag && !int.TryParse(strArray[1], out result2))
      {
        if (strArray[1] != "*")
          return false;
        result2 = asMin ? 0 : int.MaxValue;
        result3 = asMin ? 0 : int.MaxValue;
        result4 = asMin ? 0 : int.MaxValue;
        flag = true;
      }
      if (!flag && !int.TryParse(strArray[2], out result3))
      {
        if (strArray[2] != "*")
          return false;
        result3 = asMin ? 0 : int.MaxValue;
        result4 = asMin ? 0 : int.MaxValue;
        flag = true;
      }
      if (!flag && strArray.Length == 4 && !int.TryParse(strArray[3], out result4))
      {
        if (strArray[3] != "*")
          return false;
        result4 = asMin ? 0 : int.MaxValue;
      }
      ApplicationVersionType applicationVersionType = ApplicationVersion.ApplicationVersionTypeFromString(strArray[0][0].ToString());
      version = new ApplicationVersion(applicationVersionType, result1, result2, result3, result4);
      return true;
    }

    public static string ToString(ApplicationVersion av)
    {
      string prefix = ApplicationVersion.GetPrefix(av.ApplicationVersionType);
      ApplicationVersion applicationVersion = ApplicationVersion.FromParametersFile();
      return string.Format("{0}{1}.{2}.{3}{4}", (object) prefix, (object) av.Major, (object) av.Minor, (object) av.Revision, av.ChangeSet == applicationVersion.ChangeSet ? (object) "" : (object) string.Format(".{0}", (object) av.ChangeSet));
    }

    public static bool IsSame(this ApplicationVersion @this, ApplicationVersion other)
    {
      return @this.ApplicationVersionType == other.ApplicationVersionType && @this.Major == other.Major && @this.Minor == other.Minor && @this.Revision == other.Revision;
    }

    public static bool IsSameWithChangeSet(this ApplicationVersion @this, ApplicationVersion other)
    {
      return @this.ApplicationVersionType == other.ApplicationVersionType && @this.Major == other.Major && @this.Minor == other.Minor && @this.Revision == other.Revision && @this.ChangeSet == other.ChangeSet;
    }

    public static ApplicationVersion? GameVersion()
    {
      return new ApplicationVersion?(ApplicationVersion.FromParametersFile());
    }

    public static string GameVersionStr()
    {
      return ApplicationVersionHelper.ToString(ApplicationVersionHelper.GameVersion() ?? ApplicationVersion.Empty);
    }
  }
}
